From 3d157bbd06506b170fde5ec23980c4bf9f7660e2 Mon Sep 17 00:00:00 2001
From: Chris Coulson <chris.coulson@canonical.com>
Date: Thu, 7 Jan 2021 19:53:55 +0000
Subject: [PATCH] kern/parser: Introduce terminate_arg() helper

process_char() and grub_parser_split_cmdline() use similar code for
terminating the most recent argument. Add a helper function for this.

Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
---
 grub-core/kern/parser.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
index 0d3582b..572c670 100644
--- a/grub-core/kern/parser.c
+++ b/grub-core/kern/parser.c
@@ -129,6 +129,16 @@ add_var (char *varname, char **bp, char **vp,
     *((*bp)++) = *val;
 }
 
+static void
+terminate_arg (char *buffer, char **bp, int *argc)
+{
+  if (*bp != buffer && *((*bp) - 1) != '\0')
+    {
+      *((*bp)++) = '\0';
+      (*argc)++;
+    }
+}
+
 static grub_err_t
 process_char (char c, char *buffer, char **bp, char *varname, char **vp,
 	      grub_parser_state_t state, int *argc,
@@ -157,11 +167,7 @@ process_char (char c, char *buffer, char **bp, char *varname, char **vp,
        * Don't add more than one argument if multiple
        * spaces are used.
        */
-      if (*bp != buffer && *((*bp) - 1) != '\0')
-	{
-	  *((*bp)++) = '\0';
-	  (*argc)++;
-	}
+      terminate_arg (buffer, bp, argc);
     }
   else if (use)
     *((*bp)++) = use;
@@ -232,11 +238,8 @@ grub_parser_split_cmdline (const char *cmdline,
      variable.  */
   add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);
 
-  if (bp != buffer && *(bp - 1))
-    {
-      *(bp++) = '\0';
-      (*argc)++;
-    }
+  /* Ensure that the last argument is terminated. */
+  terminate_arg (buffer, &bp, argc);
 
   /* If there are no args, then we're done. */
   if (!*argc)
-- 
2.14.2

